package com.invengo.base;


import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.eftimoff.androipathview.PathView;
import com.invengo.base.router.RouterActivityPath;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import me.goldze.mvvmhabit.bus.Messenger;
import me.goldze.mvvmhabit.utils.KLog;


/**
 * @author buoker
 * @version 1.0.0
 * @date 2023/10/24
 * @className java.lang.String
 */

public class SplashActivity extends FragmentActivity {
    private static final String TAG = SplashActivity.class.getSimpleName();
    private PathView pathView;
    private String pathValue = "";

    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        Messenger.getDefault().register(this, "LAUNCH_PAGE", String.class, s -> {
            ARouter.getInstance().build(RouterActivityPath.Robot.ROBOT_MAIN).withString("path",s).navigation();
            finish();
        });
//        Messenger.getDefault().register(this, "OPEARTION_TO_PAGE", ActionEvent.class, action -> {
//            Observable.just(action)
//                    .subscribeOn(AndroidSchedulers.mainThread())
//                    .subscribe(event -> {
//                        switch (event.getEvent()) {
//                            case ActionEvent.ACTION_HOME:
//                            case ActionEvent.ACTION_BACK:
//                                finish();
//                                break;
//                        }
//                    });
//        });
        launchActivity();
        KLog.e("-----------SplashActivity");
    }

    public void close(View view) {
        finish();
    }

    String[] permissions = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
    };

    private void checkPersmission() {
        RxPermissions rxPermission = new RxPermissions(this);
        rxPermission.requestEach(permissions)
                .subscribe(permission -> {
                    if (permission.granted) {
                        // 用户已经同意该权限
                        Log.d(TAG, permission.name + " is granted.");
                        startCheck();
                    } else if (permission.shouldShowRequestPermissionRationale) {
                        // 用户拒绝了该权限，没有选中『不再询问』（Never ask again）,那么下次再次启动时，还会提示请求权限的对话框
                        System.exit(0);
                    } else {
                        Log.d(TAG, permission.name + " is denied.");
                        System.exit(0);
                    }
                });
    }

    private void startCheck() {
//        setContentView(R.layout.activity_splash);
//        pathView = findViewById(R.id.pathView);
        ARouter.getInstance().build(RouterActivityPath.Check.PAGER_LOGIN).navigation();
        finish();
    }

    private void launchActivity() {
        String packageName = this.getPackageName();
        switch (packageName) {
            case "com.invengo.module_check":
                checkPersmission();
                break;
            case "com.invengo.module_robot":
                ARouter.getInstance().build(RouterActivityPath.Robot.ROBOT_MAIN).navigation();
                finish();
//                KLog.e("开始跳转"+pathValue);
//                if (TextUtils.isEmpty(pathValue)) {
//                    ARouter.getInstance().build(RouterActivityPath.Robot.ROBOT_MAIN).navigation();
//                } else {
//                    KLog.e("开始跳转");
//                    ARouter.getInstance().build(RouterActivityPath.Robot.ROBOT_MAIN).withString("path",pathValue).navigation();
//                }
//                finish();
                break;
        }
    }


    private void startAni() {
//        pathView.getSequentialPathAnimator().delay(800).duration(4500).listenerEnd(() -> {
//                ARouter.getInstance().build(RouterActivityPath.Check.PAGER_LOGIN).navigation();
//                finish();
//        }).interpolator(new AccelerateDecelerateInterpolator()).start();
    }
}
